package com.nit.crawler;

import org.apache.commons.text.similarity.CosineSimilarity;

import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

public class CommonTxtTest {

    /**
     * 计算两个字符串的相似性
     *
     * @param args1
     * @param args2
     * @return
     */
    private static Double getSimilar(String args1, String args2) {
        Map<CharSequence, Integer> map = Arrays.stream(args1.split(""))
                .collect(Collectors.toMap(c -> c, c -> 1, Integer::sum));
        Map<CharSequence, Integer> map2 = Arrays.stream(args2.split(""))
                .collect(Collectors.toMap(c -> c, c -> 1, Integer::sum));
        CosineSimilarity cosineSimilarity = new CosineSimilarity();
        Double similar = cosineSimilarity.cosineSimilarity(map, map2);
        return similar;
    }
    public static void main(String[] args) {
        String s1 = "喜食寿司";
        String s2 = "喜食披萨。寿司";

        System.out.println(s1 + "与" + s2 + "==相似度===" + getSimilar(s1, s2));
//        System.out.println(s1 + "与" + s4 + "==相似度===" + getSimilar(s1, s4));
//        System.out.println(s1 + "与" + s3 + "==相似度===" + getSimilar(s1, s3));
//        System.out.println(s4 + "与" + s3 + "==相似度===" + getSimilar(s4, s3));
    }
}
